﻿/*
 
MIT License

Copyright (c) 2017 Chevy Ray Johnston

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

using System.Collections;
using System.Collections.Generic;

namespace reactiveBT.Coroutines
{
    /// <summary>
    /// A container for running multiple routines in parallel. Coroutines can be nested.
    /// </summary>
    public class CoroutineManager
    {
        List<Coroutine> coroutines = new List<Coroutine>();
        List<float> delays = new List<float>();

        private static CoroutineManager instance = null;
        public static CoroutineManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CoroutineManager();
                }
                return instance;
            }
        }

        public CoroutineHandle Fork(IEnumerator routine,float delay = 0)
        {
            var newCoroutine = new Coroutine(routine, delay);
            coroutines.Add(newCoroutine);
            return new CoroutineHandle(newCoroutine);
        }
        public CoroutineHandle Fork(System.Action fn,float delay = 0)
        {
            var newCoroutine = new Coroutine(fn, delay);
            coroutines.Add(newCoroutine);
            return new CoroutineHandle(newCoroutine);
        }

        /// <summary>
        /// Stop all running routines.
        /// TODO 也有点问题
        /// </summary>
        public void StopAll()
        {
            coroutines.Clear();
            //delays.Clear();
        }

        /// <summary>
        /// Update all running coroutines.
        /// </summary>
        /// <returns>True if any routines were updated.</returns>
        /// <param name="deltaTime">How many seconds have passed sinced the last update.</param>
        public bool Update(float deltaTime)
        {
            if (coroutines.Count > 0)
            {
                for (int i = 0; i < coroutines.Count; i++)
                {
/*                    if (delays[i] > 0f)
                        delays[i] -= deltaTime;
                    else if (coroutines[i] == null || !MoveNext(coroutines[i], i))
                    {
                        coroutines.RemoveAt(i);
                        delays.RemoveAt(i--);
                    }*/
                    if (coroutines[i] == null || !MoveNext(coroutines[i],deltaTime))
                    {
                        coroutines.RemoveAt(i);
                    }
                }
                return true;
            }
            return false;
        }
        /// <summary>
        /// 协程核心方法
        /// </summary>
        /// <param name="routine">协程对象</param>
        /// <param name="deltaTime">每帧时间</param>
        /// <returns></returns>
        bool MoveNext(Coroutine routine,float deltaTime)
        {
            switch(routine.state)
            {
                case CoroutineState.Dead:
                    return false;
                case CoroutineState.Suspended:
                    return true;
                default:
                    if (routine.MoveNext(deltaTime))
                    {
                        routine.state = CoroutineState.Running;
                        return true;
                    }
                    routine.state = CoroutineState.Dead;
                    return false;
            }
        }
    }
}